 // 首页的登录守卫
 if(localStorage.getItem("token") === "ok"){
    $(".p1").hide()
    
    $(".p2").show().children("span").html(localStorage.getItem("un"))
}

// // 退出功能
$(".exit").click(function(){
    localStorage.setItem("token","");
    $(".p1").show()
    $(".p2").hide()
})


// /* 登录守卫 */
var goCart = document.getElementById("goCart");
console.log(goCart);
// 去购物车的登录守卫
goCart.onclick = function(){
    if(localStorage.getItem("token") !== "ok"){
        if(confirm("您好，请您先登录")){
            location.href = "./login.html"
        }
        return false;
    }
}




/* 楼层部分 */
$(".btns").children("span").click(function () {
    $("html").animate({
        scrollTop: $(".floor").eq($(this).index()).offset().top
    })
})
/* 百度接口搜索栏 */
//获取元素
const obtn = document.querySelector(".search-word");
const otxt = document.querySelector(".search");
const oul = document.getElementById("list");
var input = document.querySelectorAll("input");

// 点击按钮，跳转到真实的百度搜索
obtn.onclick = function () {
    location.href = "https://www.baidu.com/s?wd=" + otxt.value;
}

otxt.oninput = function () {
    // 判断输入框是否为空，为空，清除下拉菜单
    if (this.value === "") {
        oul.innerHTML = "";
        oul.style.display = "none";
    } else {
        oul.style.display = "block"
    }
    // console.log(this.value)
    // 开启jsonp请求，请求百度数据
    jsonp("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su", function (res) {
        // 请求成功后，处理数据
        // console.log(res.s);
        let str = "";
        // 拼接页面结构
        for (let i = 0; i < res.s.length; i++) {
            str += `<li>${res.s[i]}</li>`
        }
        // 渲染页面
        // console.log(str)
        oul.innerHTML = str;
    }, {
        wd: this.value,
        cb: "ajshdfgausdf",
        selfName: "cb"
    })
}

function jsonp(url, cb, data) {
    let str = "";
    for (let i in data) {
        str += `${i}=${data[i]}&`
    }
    url += "?" + str + "_qft_=" + Date.now();
    const script = document.createElement("script");
    script.src = url;
    document.body.appendChild(script);

    window[data[data.selfName]] = function (res) {
        cb(res);
    }
}


/* 轮播图 */
var wrap = document.querySelector(".wrap");
var next = document.querySelector(".arrow_right");
var prev = document.querySelector(".arrow_left");
next.onclick = function () {
    next_pic();
}
prev.onclick = function () {
    prev_pic();
}
function next_pic () {
    index++;
    if(index > 4){
        index = 0;
    }
    showCurrentDot();
    var newLeft;
    if(wrap.style.left === "-7680px"){
        newLeft = -2560;
    }else{
        newLeft = parseInt(wrap.style.left)-1280;
    }
    wrap.style.left = newLeft + "px";
}
function prev_pic () {
    index--;
    if(index < 0){
        index = 4;
    }
    showCurrentDot();
    var newLeft;
    if(wrap.style.left === "0px"){
        newLeft = -5120;
    }else{
        newLeft = parseInt(wrap.style.left)+1280;
    }
    wrap.style.left = newLeft + "px";
}
var timer = null;
function autoPlay () {
    timer = setInterval(function () {
        next_pic();
    },2000);
}
autoPlay();

var container = document.querySelector(".container");
container.onmouseenter = function () {
    clearInterval(timer);
}
container.onmouseleave = function () {
    autoPlay();    
}

var index = 0;
var buttons = document.querySelector(".buttons");
var dots = buttons.getElementsByTagName("span");
function showCurrentDot () {
    for(var i = 0, len = dots.length; i < len; i++){
        dots[i].className = "";
    }
    dots[index].className = "on";
}

for (var i = 0, len = dots.length; i < len; i++){
    (function(i){
        dots[i].onclick = function () {
            var dis = index - i;
            if(index == 4 && parseInt(wrap.style.left)!==-6400){
                dis = dis - 5;     
            }
            //和使用prev和next相同，在最开始的照片5和最终的照片1在使用时会出现问题，导致符号和位数的出错，做相应地处理即可
            if(index == 0 && parseInt(wrap.style.left)!== -1280){
                dis = 5 + dis;
            }
            wrap.style.left = (parseInt(wrap.style.left) +  dis * 1280)+"px";
            index = i;
            showCurrentDot();
        }
    })(i);            
}

/* 我的聚美下拉菜单 */
//获取元素
let topright = document.getElementsByClassName("top-right")[0];
let mylist = document.getElementsByClassName("my-list")[0];
let mydiv1 = topright.getElementsByClassName("my")[0];
//绑定事件
mydiv1.addEventListener("onmouseout", function () {
    mylist.style.display = "none";
})

/* 更多下拉菜单 */
//获取元素
let morelist = document.getElementsByClassName("more-list")[0];
let mydiv2 = topright.getElementsByClassName("more")[0];
//绑定事件
mydiv2.addEventListener("onmouseout", function () {
    morelist.style.display = "none";
})


/* 多级菜单 */
//获取元素
let navlist = document.getElementsByClassName("nav-list")[0];
let li4 = navlist.children[4];
let menu = document.getElementsByClassName("menu")[0];
//添加事件
let flag2 = 0;
li4.addEventListener("click", function () {
    if (flag2 == 0) {
        menu.style.display = "block";
        flag2 = 1;
    } else {
        menu.style.display = "none";
        flag2 = 0;
    }
})

/* 多级菜单 */
//获取元素
let goodsright = document.getElementsByClassName("goods-right");
let goodslist = document.getElementsByClassName("goods-list")[0];
let op = goodslist.getElementsByTagName("p");
for (var i = 0; i < op.length; i++) {
    op[i].xuhao = i;
    op[i].onmouseover = function () {
        for (var j = 0; j < op.length; j++) {
            op[j].style.hover = "none";
            goodsright[j].style.display = "none";
        }
        this.style.hover = "red";
        goodsright[this.xuhao].style.display = "block";
    }
    op[i].onmouseleave = function () {
        goodsright[this.xuhao].style.display = "none";
    }
}
for (var i = 0; i < goodsright.length; i++) {
    goodsright[i].onmouseleave = function () {
        this.style.display = "none";
    }
    goodsright[i].onmouseover = function () {
        this.style.display = "block";
    }
}